#include "pch.hpp"
#include "D3D11Effect.hpp"
#include "D3D11Exception.hpp"

using namespace Engine;
using namespace Engine::Video;




D3D11Effect::D3D11Effect(const DestroyedFn& destroyedFn, const ID3D11DevicePtr& device, const QString& name)
	: m_destroyedFn(destroyedFn)
	, m_optimized(false)
	, m_device(device)
	, m_name(name)
{}
///////////////////////////////////////////////////////////////////////////////////////////////////

void D3D11Effect::post_create(ptr _this)
{
	m_this = _this;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

D3D11Effect::~D3D11Effect()
{
	m_destroyedFn(m_this);
}
///////////////////////////////////////////////////////////////////////////////////////////////////




void D3D11Effect::draw(D3D11DeviceContext& context, const RenderNode::ptr& node)
{
	TechniqueMap::iterator i = m_techniques.find(node->technique);
	if(i == m_techniques.end())
		BOOSTEXT_THROW(exception(format("Unable to Draw RenderNode: The effect '%1%' doesn't have a technique named '%2%'") % m_name % node->technique));

	i->second.draw(m_device, context, node);
}
///////////////////////////////////////////////////////////////////////////////////////////////////




EffectTechnique& D3D11Effect::addTechnique(const std::string& name)
{
	if(m_techniques.find(name) != m_techniques.end())
		BOOSTEXT_THROW(exception(format("There is already a technique '%1%' in the effect '%2%'") % name % m_name));

	return m_techniques.insert(std::make_pair(name, D3D11EffectTechnique(m_device))).first->second;
}
///////////////////////////////////////////////////////////////////////////////////////////////////




void D3D11Effect::optimize()
{
	foreach(TechniquePair& technique, m_techniques)
		technique.second.optimize();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
